/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ddd.service;

import com.ddd.context.Parameters;
import com.ddd.entity.Negoitem;
import com.ddd.util.HibernateUtil;
import com.ddd.util.Util;
import java.util.ArrayList;
import java.util.Iterator;
import javax.servlet.http.HttpServletRequest;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.criterion.Restrictions;

public class NegoitemService extends BaseService {

    private Parameters para;
    private LogService logService;

    public NegoitemService(HttpServletRequest request) {
        para = new Parameters(request);
        logService = new LogService(request);
    }

    public Negoitem load(String id) {
        SessionFactory factory = HibernateUtil.getSessionFactory();
        Session session = factory.openSession();

        Transaction tx = session.beginTransaction();

        Negoitem negoitem = (Negoitem) session.load(Negoitem.class, id);
        //Hibernate.initialize(journal);

        tx.commit();
        session.close();

        return negoitem;
    }

    public ArrayList loadAll() {
        ArrayList result = new ArrayList();
        SessionFactory factory = HibernateUtil.getSessionFactory();
        Session session = factory.openSession();

        Transaction tx = session.beginTransaction();
        Criteria criteria = session.createCriteria(Negoitem.class);
        Iterator iter = criteria.list().iterator();
        while (iter.hasNext()) {
            result.add(iter.next());
        }
        tx.commit();
        session.close();
        para.setTotalSize(result.size());
        return result;
    }

    public void save(Negoitem negoitem) {
        // get id first
        IDService idService = new IDService();
        negoitem.setId(idService.getNextID("negoitem"));
        
        SessionFactory factory = HibernateUtil.getSessionFactory();
        Session session = factory.openSession();

        Transaction tx = session.beginTransaction();
        negoitem.setCid(para.getUsername());
        negoitem.setCtime(Util.getTodayWithTime());
        session.save(negoitem);
        tx.commit();
        session.close();
    }

    public void update(Negoitem negoitem) {
        SessionFactory factory = HibernateUtil.getSessionFactory();
        Session session = factory.openSession();

        Transaction tx = session.beginTransaction();
        negoitem.setUid(para.getUsername());
        negoitem.setUtime(Util.getTodayWithTime());
        session.update(negoitem);
        tx.commit();
        session.close();
    }

    public void delete(Negoitem negoitem) {
        SessionFactory factory = HibernateUtil.getSessionFactory();
        Session session = factory.openSession();

        Transaction tx = session.beginTransaction();
        session.delete(negoitem);
        tx.commit();
        session.close();
    }

    public ArrayList loadByNegotiationID(String negoID) {
        ArrayList result = new ArrayList();
        SessionFactory factory = HibernateUtil.getSessionFactory();
        Session session = factory.openSession();

        Transaction tx = session.beginTransaction();
        Criteria criteria = session.createCriteria(Negoitem.class);
        criteria.add(Restrictions.eq("fkNegotiationId" , negoID));
        
        Iterator iter = criteria.list().iterator();
        while (iter.hasNext()) {
            result.add(iter.next());
        }
        tx.commit();
        session.close();
        //para.setTotalSize(result.size());
        return result;
    }
}
